package com.github.binarylei.hello_world.bolt;

import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseBasicBolt;
import org.apache.storm.tuple.Tuple;

import java.io.FileWriter;

/**
 * @author: leigang
 * @version: 2018-04-07
 */
public class WriteBolt extends BaseBasicBolt {

    //private static final Log logger = LoggerFactory.getLog(PrintBolt.class);

    private static final long serialVersionUID = 1L;

    private FileWriter writer;

    @Override
    public void execute(Tuple input, BasicOutputCollector collector) {
        // 获取上一个组件声明的Field
        String text = input.getStringByField("write");

        try {
            if (writer == null) {
                this.writer = new FileWriter("d:/test/" + this);
            }
            System.out.println("写入文件...");
            writer.write(text);
            writer.write("\n");
            writer.flush();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {

    }
}
